<?php defined("MAIN_DIR") or die("No direct access");

$add_error = false;

if (isset($_POST['action']) && $_POST['action'] == "add")
{
	$bank = filter_var($_POST['bank'],FILTER_SANITIZE_MAGIC_QUOTES);
	$regionally = $route->POST('regionally') ? 1 : 0;
	if ($db->getCell("select `parent` from `bank` where `bank_id` = '$bank' limit 1")) {
		$regionally = 0;
	}
	$account_opening = isset($_POST['by_agree_open']) ? 0 : filter_var($_POST['account_opening'],FILTER_SANITIZE_MAGIC_QUOTES);
	$account_maintenance_low = filter_var($_POST['account_maintenance_low'],FILTER_SANITIZE_MAGIC_QUOTES);
	$account_maintenance_high = filter_var($_POST['account_maintenance_high'],FILTER_SANITIZE_MAGIC_QUOTES);
	
	if (isset($_POST['by_agree_inet'])) {
		$internet_banking_low = $internet_banking_high = 0;
	} else {
		$internet_banking_low = filter_var($_POST['internet_banking_low'],FILTER_SANITIZE_MAGIC_QUOTES);
		$internet_banking_high = filter_var($_POST['internet_banking_high'],FILTER_SANITIZE_MAGIC_QUOTES);
	}
	
	if (isset($_POST['by_agree_transfer'])) {
		$transfer_low = $transfer_high = 0;
	} else {
		$transfer_low = filter_var($_POST['transfer_low'],FILTER_SANITIZE_MAGIC_QUOTES);
		$transfer_high = filter_var($_POST['transfer_high'],FILTER_SANITIZE_MAGIC_QUOTES);
	}
	
	$cash_low = filter_var($_POST['cash_low'],FILTER_SANITIZE_MAGIC_QUOTES);
	$cash_high = trim(filter_var($_POST['cash_high'],FILTER_SANITIZE_MAGIC_QUOTES));
	$receiving_cash_low = trim(filter_var($_POST['receiving_cash_low'],FILTER_SANITIZE_MAGIC_QUOTES));
	$receiving_cash_high = trim(filter_var($_POST['receiving_cash_high'],FILTER_SANITIZE_MAGIC_QUOTES));
	$add_query = "insert into `rko` (`bank_id`,`regionally`,`account_opening`,`account_maintenance_low`,`account_maintenance_high`,`internet_banking_low`,`internet_banking_high`,`transfer_low`,`transfer_high`,`cash_low`,`cash_high`,`receiving_cash_low`,`receiving_cash_high`) values ('$bank','$regionally','$account_opening','$account_maintenance_low','$account_maintenance_high','$internet_banking_low','$internet_banking_high','$transfer_low','$transfer_high','$cash_low','$cash_high','$receiving_cash_low','$receiving_cash_high')";
	if (mysql_query($add_query)) {
		$rko_id = mysql_insert_id();
		to("/admin/rko/edit/$rko_id");
	} else {
		$add_error = true;echo mysql_error();
	}
}

require_once ADMIN.HEADFILE;
?>
<script type="text/javascript">
$(document).ready(function(){
	$("#by_agree_open").click(function(){
		if ($(this).attr("checked"))
			$("#account_opening").attr("disabled","disabled");
		else
			$("#account_opening").removeAttr("disabled");
	})
	$("#by_agree_inet").click(function(){
		if ($(this).attr("checked"))
			$("#internet_banking input").not(this).attr("disabled","disabled");
		else
			$("#internet_banking input").not(this).removeAttr("disabled");
	})
	$("#by_agree_transfer").click(function(){
		if ($(this).attr("checked"))
			$("#transfer input").not(this).attr("disabled","disabled");
		else
			$("#transfer input").not(this).removeAttr("disabled");
	})
	$("#bank").change(function(){
		if ($("#bank option:selected").hasClass("branch")) {
			$("#regionally").attr("disabled","disabled").removeAttr("checked");
		} else {
			$("#regionally").removeAttr("disabled");
		}
	})
	if ($("#bank")[0].selectedIndex < 3)
		$("#bank")[0].selectedIndex = 3;
})
</script>
<div align="left">
<h1>Добавить РКО</h1>
<hr /><?php
if ($add_error)
	echo "<p style='color:red'>Не удалось добавить</p>";
?><form method='POST'>
<input type='hidden' name='action' value='add' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank' id='bank'>
				<?php
				$bankslist = $db->getTable("select `bank_id`,`name` from `bank` where `parent` = '0' order by `name`");
				echo "<option disabled='disabled' style='color:gray'>———————————————</option>
						<option disabled='disabled' style='color:gray'>Головные офисы</option>
						<option disabled='disabled' style='color:gray'>———————————————</option>";
				if ($bankslist) {
					
					foreach ($bankslist as $bank) {
						
						echo "<option value='$bank[bank_id]'";
						if ($bank['bank_id'] == $route->POST('bank_id'))
							echo " selected='selected'";
						echo ">$bank[name]</option>";
					}
				} else {
					echo "<option disabled='disabled' style='color:gray'>В данном городе нет головных офисов</option>";
				}
				$branches = $db->getTable("select `bank_id`,`name` from `bank` where `parent` != '0' and `city_id` = '$_SESSION[city_admin]' order by `name`");
				echo "<option disabled='disabled' style='color:gray'>———————————————</option>
						<option disabled='disabled' style='color:gray'>Филиалы</option>
						<option disabled='disabled' style='color:gray'>———————————————</option>";
				$regional = false;
				if ($branches) {
					
					foreach ($branches as $branch) {
						
						echo "<option class='branch' value='$branch[bank_id]'";
						if ($branch['bank_id'] == $route->POST('bank_id')) {
							echo " selected='selected'";
							$regional = true;
						}
						echo ">$branch[name]</option>";
					}
				} else {
					echo "<option disabled='disabled' style='color:gray'>В данном городе нет филиалов</option>";
				}
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Межрегионально</td>
		<td colspan='2' align="left">
			<input type='checkbox' name='regionally' id='regionally' <?php
				if ($route->POST('regionally')) {
					echo "checked='checked'";
				}
				if ($regional) {
					echo "disabled='disabled'";
				}
			?> />
		</td>
	</tr>
	<tr>
		<td>Открытие (руб.)</td>
		<td colspan='2'>
			<input id="account_opening" style='width:200px' type='text' name='account_opening' value='<?php if (isset($_POST['account_opening'])) echo $_POST['account_opening'];?>'<?php if (isset($_POST['by_agree_open'])) echo " disabled";?> />
			<input type="checkbox" name='by_agree_open' id="by_agree_open"<?php if (isset($_POST['by_agree_open'])) echo " checked='checked'";?> /> Бесплатно
		</td>
	</tr>
	<tr>
		<td>Ведение (руб.)</td>
		<td width="150px">
			от <input style='width:120px' type='text' name='account_maintenance_low' value='<?php if (isset($_POST['account_maintenance_low'])) echo $_POST['account_maintenance_low'];?>' />
		</td>
		<td width="350px">
			до <input style='width:120px' type='text' name='account_maintenance_high' value='<?php if (isset($_POST['account_maintenance_high'])) echo $_POST['account_maintenance_high'];?>' />
		</td>
	</tr>
	<tr id="internet_banking">
		<td>Интернет-банкинг (руб.)</td>
		<td width="150px">
			от <input style='width:120px' type='text' name='internet_banking_low' value='<?php if (isset($_POST['internet_banking_low'])) echo $_POST['internet_banking_low'];?>'<?php if (isset($_POST['by_agree_inet'])) echo " disabled";?> />
		</td>
		<td>
			до <input style='width:120px' type='text' name='internet_banking_high' value='<?php if (isset($_POST['internet_banking_high'])) echo $_POST['internet_banking_high'];?>'<?php if (isset($_POST['by_agree_inet'])) echo " disabled";?> />
			<input type="checkbox" name='by_agree_inet' id="by_agree_inet"<?php if (isset($_POST['by_agree_inet'])) echo " checked='checked'";?> /> Бесплатно
		</td>
	</tr>
	<tr>
		<td>Перевод (руб.)</td>
		<td>
			от <input style='width:120px' type='text' name='transfer_low' value='<?php if (isset($_POST['transfer_low'])) echo $_POST['transfer_low'];?>' />
		</td>
		<td>
			до <input style='width:120px' type='text' name='transfer_high' value='<?php if (isset($_POST['transfer_high'])) echo $_POST['transfer_high'];?>' />
		</td>
	</tr>
	<tr>
		<td>Выдача (%)</td>
		<td>
			от <input style='width:120px' type='text' name='cash_low' value='<?php if (isset($_POST['cash_low'])) echo $_POST['cash_low'];?>' />
		</td>
		<td>
			до <input style='width:120px' type='text' name='cash_high' value='<?php if (isset($_POST['cash_high'])) echo $_POST['cash_high'];?>' />
		</td>
	</tr>
	<tr>
		<td>Прием (%)</td>
		<td>
			от <input style='width:120px' type='text' name='receiving_cash_low' value='<?php if (isset($_POST['receiving_cash_low'])) echo $_POST['receiving_cash_low'];?>' />
		</td>
		<td>
			до <input style='width:120px' type='text' name='receiving_cash_high' value='<?php if (isset($_POST['receiving_cash_high'])) echo $_POST['receiving_cash_high'];?>' />
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='3' align="right"><button style="width:100px;height:30px" type="submit">Добавить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>